|
1
|
|
|
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ |
|
2
|
|
|
/* vim: set ts=2: */ |
|
3
|
|
|
|
|
4
|
|
|
import { Component } from "@angular/core"; |
|
5
|
|
|
import * as dockModule from "tns-core-modules/ui/layouts/dock-layout"; |
|
6
|
|
|
import * as buttonModule from "tns-core-modules/ui/button"; |
|
7
|
|
|
import * as textModule from "tns-core-modules/text"; |
|
8
|
|
|
import * as dialogs from "ui/dialogs"; |
|
9
|
|
|
import * as fs from "tns-core-modules/file-system"; |
|
10
|
|
|
|
|
11
|
|
|
/* NativeScript does not support import syntax for npm modules */ |
|
12
|
|
|
const XLSX = require("./xlsx.full.min.js"); |
|
13
|
|
|
|
|
14
|
|
|
@Component({ |
|
15
|
|
|
selector: "my-app", |
|
16
|
|
|
template: ` |
|
17
|
|
|
<GridLayout rows="auto, *, auto"> |
|
18
|
|
|
<ActionBar row="0" title="SheetJS NativeScript Demo" class="action-bar"></ActionBar> |
|
19
|
|
|
|
|
20
|
|
|
<!-- data converted to HTML and rendered in web view --> |
|
21
|
|
|
<WebView row="1" src="{{html}}"></WebView> |
|
22
|
|
|
|
|
23
|
|
|
<DockLayout row="2" dock="bottom" stretchLastChild="false"> |
|
24
|
|
|
<Button text="Import File" (tap)="import()" style="padding: 10px"></Button> |
|
25
|
|
|
<Button text="Export File" (tap)="export()" style="padding: 10px"></Button> |
|
26
|
|
|
</DockLayout> |
|
27
|
|
|
</GridLayout> |
|
28
|
|
|
` |
|
29
|
|
|
}) |
|
30
|
|
|
|
|
31
|
|
|
export class AppComponent { |
|
32
|
|
|
html: string = ""; |
|
33
|
|
|
constructor() { |
|
34
|
|
|
const ws = XLSX.utils.aoa_to_sheet([[1,2],[3,4]]); |
|
35
|
|
|
this.html = XLSX.utils.sheet_to_html(ws); |
|
36
|
|
|
}; |
|
37
|
|
|
|
|
38
|
|
|
/* Import button */ |
|
39
|
|
|
async import() { |
|
40
|
|
|
const filename: string = "SheetJSNS.xlsx"; |
|
41
|
|
|
|
|
42
|
|
|
/* find appropriate path */ |
|
43
|
|
|
const target: fs.Folder = fs.knownFolders.documents() || fs.knownFolders.ios.sharedPublic(); |
|
44
|
|
|
const url: string = fs.path.normalize(target.path + "///" + filename); |
|
45
|
|
|
const file: fs.File = fs.File.fromPath(url); |
|
46
|
|
|
|
|
47
|
|
|
try { |
|
48
|
|
|
/* get binary string */ |
|
49
|
|
|
const bstr: string = await file.readText(textModule.encoding.ISO_8859_1); |
|
50
|
|
|
|
|
51
|
|
|
/* read workbook */ |
|
52
|
|
|
const wb = XLSX.read(bstr, { type: "binary" }); |
|
53
|
|
|
|
|
54
|
|
|
/* grab first sheet */ |
|
55
|
|
|
const wsname: string = wb.SheetNames[0]; |
|
56
|
|
|
const ws = wb.Sheets[wsname]; |
|
57
|
|
|
|
|
58
|
|
|
/* update table */ |
|
59
|
|
|
this.html = XLSX.utils.sheet_to_html(ws); |
|
60
|
|
|
dialogs.alert(`Attempting to read to SheetJSNS.xlsx in ${url}`); |
|
61
|
|
|
} catch(e) { |
|
62
|
|
|
dialogs.alert(e.message); |
|
63
|
|
|
} |
|
64
|
|
|
}; |
|
65
|
|
|
|
|
66
|
|
|
/* Export button */ |
|
67
|
|
|
async export() { |
|
68
|
|
|
const wb = XLSX.read(this.html, { type: "string" }); |
|
69
|
|
|
const filename: string = "SheetJSNS.xlsx"; |
|
70
|
|
|
|
|
71
|
|
|
/* generate binary string */ |
|
72
|
|
|
const wbout: string = XLSX.write(wb, { bookType: 'xlsx', type: 'binary' }); |
|
73
|
|
|
|
|
74
|
|
|
/* find appropriate path */ |
|
75
|
|
|
const target: fs.Folder = fs.knownFolders.documents() || fs.knownFolders.ios.sharedPublic(); |
|
76
|
|
|
const url: string = fs.path.normalize(target.path + "///" + filename); |
|
77
|
|
|
const file: fs.File = fs.File.fromPath(url); |
|
78
|
|
|
|
|
79
|
|
|
/* attempt to save binary string to file */ |
|
80
|
|
|
await file.writeText(wbout, textModule.encoding.ISO_8859_1); |
|
81
|
|
|
dialogs.alert(`Wrote to SheetJSNS.xlsx in ${url}`); |
|
82
|
|
|
}; |
|
83
|
|
|
} |
|
84
|
|
|
|